package com.zhuozhou.jdyc.main.meinfo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityShipingAddressBinding;
import com.zhuozhou.jdyc.main.meinfo.adapter.AddressAdapter;
import com.zhuozhou.jdyc.main.meinfo.bean.ShippingAddressBean;
import com.zhuozhou.jdyc.main.meinfo.viewmodel.MeInfoViewModel;
import com.zhuozhou.jdyc.util.log.ToastUtils;
import com.zhuozhou.jdyc.wight.dialog.ConfirmDialog;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import static com.zhuozhou.jdyc.main.settlement.activity.ShopSettlementActivity.SHOPSETTLEMENT;

/**
 * Create by Once_ChuXy on 2021/10/20
 * Describe:收货地址
 */
public class ShippingAddressActivity extends BaseActivity<MeInfoViewModel, ActivityShipingAddressBinding>
        implements AddressAdapter.OnItemButtonClickListener, AddressAdapter.OnItemClickListener, AddressAdapter.OnItemDelClickListener {
    AddressAdapter addressAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shiping_address;
    }

    @Override
    public String setTitleBar() {
        return "收货地址";
    }

    @Override
    public void initView() {
        mViewModel.MySiteMutableLiveData.observe(this, v -> {
            addressAdapter = new AddressAdapter(this);
            addressAdapter.setList(v.getArray());
            addressAdapter.setOnItemButtonClickListener(this);
            addressAdapter.setOnItemDelClickListener(this);
//            if (baseBundle.getBoolean("isSettlement", false))
            addressAdapter.setOnItemClickListener(this);
            binding.mRecyclerViewCheck.setAdapter(addressAdapter);
            binding.mRecyclerViewCheck.setLayoutManager(new MyLayoutManager(this));
        });
        mViewModel.SiteDelMutableLiveData.observe(this, v -> {
            ToastUtils.showShort("删除成功");
            mViewModel.getMySite(UserAccountHelper.getUserID() + "");
        });

        binding.tvAddAddress.setOnClickListener(v -> {
            Intent in = new Intent(this, AddShippingAddressActivity.class);
            startActivity(in);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mViewModel.getMySite(UserAccountHelper.getUserID() + "");
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getMySite(UserAccountHelper.getUserID() + "");
    }

    @Override
    public void OnItemButtonClick(ShippingAddressBean.ArrayBean bean) {
        Intent in = new Intent(this, AddShippingAddressActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", bean);
        in.putExtras(bundle);
        startActivity(in);
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent in = new Intent(this, AddShippingAddressActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", addressAdapter.getList().get(position));
        setResult(SHOPSETTLEMENT, in);
        finish();
    }

    @Override
    public void OnItemDelClick(ShippingAddressBean.ArrayBean bean) {
        new ConfirmDialog(this)
                .setMessage("确定要删除该收货地址吗？")
                .setOnSureClickListener(dialog -> {
                    mViewModel.getSiteDel(bean.getId(), UserAccountHelper.getUserID() + "");
                })
                .builder()
                .show();
    }
}
